package com.google.android.exoplayer2.source;

import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.huawei.hwCloudJs.d.d;
import java.io.IOException;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import o.abr;
import o.abr.e;
import o.ahv;
import o.ahx;
import o.aif;
import o.aih;
import o.aim;
import o.aim.a;
import o.alc;
import o.alx;
import o.amm;

public final class ClippingMediaSource extends ahv<Void> {
    private final boolean ʻ;
    @Nullable
    private Object ʼ;
    private final e ʽ;
    private final long ˊ;
    private long ˊॱ;
    private final long ˋ;
    private IllegalClippingException ˋॱ;
    private final aim ˎ;
    private final boolean ˏ;
    private final boolean ॱ;
    private long ॱˊ;
    private c ॱॱ;
    private final ArrayList<ahx> ᐝ;

    public static final class IllegalClippingException extends IOException {
        public final int ˊ;

        @Documented
        @Retention(RetentionPolicy.SOURCE)
        public @interface Reason {
        }

        public IllegalClippingException(int i) {
            super("Illegal clipping: " + ˏ(i));
            this.ˊ = i;
        }

        private static String ˏ(int i) {
            switch (i) {
                case 0:
                    return "invalid period count";
                case 1:
                    return "not seekable to start";
                case 2:
                    return "start exceeds end";
                default:
                    return d.b;
            }
        }
    }

    static final class c extends aif {
        private final boolean ʽ;
        private final long ˋ;
        private final long ˎ;
        private final long ˏ;

        public c(abr o_abr, long j, long j2) throws IllegalClippingException {
            super(o_abr);
            if (o_abr.ˎ() != 1) {
                throw new IllegalClippingException(0);
            }
            e ˎ = o_abr.ˎ(0, new e());
            long max = Math.max(0, j);
            long max2 = j2 == Long.MIN_VALUE ? ˎ.ʻ : Math.max(0, j2);
            if (ˎ.ʻ != -9223372036854775807L) {
                if (max2 > ˎ.ʻ) {
                    max2 = ˎ.ʻ;
                }
                if (max != 0 && !ˎ.ॱ) {
                    throw new IllegalClippingException(1);
                } else if (max > max2) {
                    throw new IllegalClippingException(2);
                }
            }
            this.ˏ = max;
            this.ˋ = max2;
            this.ˎ = max2 == -9223372036854775807L ? -9223372036854775807L : max2 - max;
            boolean z = ˎ.ˋ && (max2 == -9223372036854775807L || (ˎ.ʻ != -9223372036854775807L && max2 == ˎ.ʻ));
            this.ʽ = z;
        }

        public e ˊ(int i, e eVar, boolean z, long j) {
            long j2;
            this.ॱ.ˊ(0, eVar, z, 0);
            eVar.ॱॱ += this.ˏ;
            eVar.ʻ = this.ˎ;
            eVar.ˋ = this.ʽ;
            if (eVar.ʼ != -9223372036854775807L) {
                eVar.ʼ = Math.max(eVar.ʼ, this.ˏ);
                if (this.ˋ == -9223372036854775807L) {
                    j2 = eVar.ʼ;
                } else {
                    j2 = Math.min(eVar.ʼ, this.ˋ);
                }
                eVar.ʼ = j2;
                eVar.ʼ -= this.ˏ;
            }
            j2 = C.ˋ(this.ˏ);
            if (eVar.ˎ != -9223372036854775807L) {
                eVar.ˎ += j2;
            }
            if (eVar.ˏ != -9223372036854775807L) {
                eVar.ˏ = j2 + eVar.ˏ;
            }
            return eVar;
        }

        public o.abr.c ॱ(int i, o.abr.c cVar, boolean z) {
            long j = -9223372036854775807L;
            this.ॱ.ॱ(0, cVar, z);
            long ˊ = cVar.ˊ() - this.ˏ;
            if (this.ˎ != -9223372036854775807L) {
                j = this.ˎ - ˊ;
            }
            return cVar.ˏ(cVar.ˊ, cVar.ˋ, 0, j, ˊ);
        }
    }

    public /* synthetic */ void ˋ(Object obj, aim o_aim, abr o_abr, @Nullable Object obj2) {
        ˊ((Void) obj, o_aim, o_abr, obj2);
    }

    public /* synthetic */ long ˎ(Object obj, long j) {
        return ˏ((Void) obj, j);
    }

    public void ॱ(@Nullable alx o_alx) {
        super.ॱ(o_alx);
        ˊ(null, this.ˎ);
    }

    public void ॱ() throws IOException {
        if (this.ˋॱ != null) {
            throw this.ˋॱ;
        }
        super.ॱ();
    }

    public aih ˊ(a aVar, alc o_alc, long j) {
        aih o_ahx = new ahx(this.ˎ.ˊ(aVar, o_alc, j), this.ॱ, this.ॱˊ, this.ˊॱ);
        this.ᐝ.add(o_ahx);
        return o_ahx;
    }

    public void ॱ(aih o_aih) {
        amm.ˏ(this.ᐝ.remove(o_aih));
        this.ˎ.ॱ(((ahx) o_aih).ˊ);
        if (this.ᐝ.isEmpty() && !this.ˏ) {
            ˏ(this.ॱॱ.ॱ);
        }
    }

    public void ˎ() {
        super.ˎ();
        this.ˋॱ = null;
        this.ॱॱ = null;
    }

    protected void ˊ(Void voidR, aim o_aim, abr o_abr, @Nullable Object obj) {
        if (this.ˋॱ == null) {
            this.ʼ = obj;
            ˏ(o_abr);
        }
    }

    private void ˏ(abr o_abr) {
        long j;
        long ˊ;
        long j2 = Long.MIN_VALUE;
        o_abr.ˎ(0, this.ʽ);
        long ˎ = this.ʽ.ˎ();
        if (this.ॱॱ == null || this.ᐝ.isEmpty() || this.ˏ) {
            j = this.ˊ;
            long j3 = this.ˋ;
            if (this.ʻ) {
                ˊ = this.ʽ.ˊ();
                j3 += ˊ;
                ˊ = j + ˊ;
                j = j3;
            } else {
                ˊ = j;
                j = j3;
            }
            this.ॱˊ = ˎ + ˊ;
            this.ˊॱ = this.ˋ == Long.MIN_VALUE ? Long.MIN_VALUE : ˎ + j;
            int size = this.ᐝ.size();
            for (int i = 0; i < size; i++) {
                ((ahx) this.ᐝ.get(i)).ˎ(this.ॱˊ, this.ˊॱ);
            }
            long j4 = j;
            j = ˊ;
            ˊ = j4;
        } else {
            j = this.ॱˊ - ˎ;
            if (this.ˋ != Long.MIN_VALUE) {
                j2 = this.ˊॱ - ˎ;
            }
            ˊ = j2;
        }
        try {
            this.ॱॱ = new c(o_abr, j, ˊ);
            ˏ(this.ॱॱ, this.ʼ);
        } catch (IllegalClippingException e) {
            this.ˋॱ = e;
        }
    }

    protected long ˏ(Void voidR, long j) {
        if (j == -9223372036854775807L) {
            return -9223372036854775807L;
        }
        long ˋ = C.ˋ(this.ˊ);
        long max = Math.max(0, j - ˋ);
        if (this.ˋ != Long.MIN_VALUE) {
            return Math.min(C.ˋ(this.ˋ) - ˋ, max);
        }
        return max;
    }
}
